#include "protreethread.h"
#include<Qdir>
#include<QDebug>
#include"const.h"
#include"protreeitem.h"

ProTreeThread::ProTreeThread(const QString &src_path, const QString &dist_path, QTreeWidgetItem *parent_item,
         int file_count, QTreeWidget *self, QTreeWidgetItem *root, QObject *parent)
         :QThread(parent), _src_path(src_path), _dist_path(dist_path), _file_count(file_count), _parent_item(parent_item), _self(self),
         _root(root), _bstop(false)
{

}

ProTreeThread::~ProTreeThread()
{

}

void ProTreeThread::run()
{
    CreateProTree(_src_path, _dist_path, _parent_item, _file_count, _self, _root);
    if(_bstop)
    {
        QString path = dynamic_cast<ProTreeItem*>(_root)->GetPath();
        auto index = _self->indexOfTopLevelItem(_root);
        delete _self->takeTopLevelItem(index);
        QDir dir(path);
        dir.removeRecursively();
        return;
    }
    emit SigFinishProgress(_file_count);
}

void ProTreeThread::CreateProTree(const QString &src_path, const QString &dist_path, QTreeWidgetItem *parent_item, int &file_count, QTreeWidget *self, QTreeWidgetItem *root, QTreeWidgetItem *pre_item)
{
    if(_bstop)
    {
        return;
    }
    bool needcopy = true;
//    如果是同一个目录，那不需要拷贝，那这个后面的逻辑还有什么意义？为什么不直接return掉
    if(src_path == dist_path)
    {
        needcopy = false;
    }

    QDir import_dir(src_path);
    QStringList nameFilters;
    //    需要文件夹，文件，和非(.,..)[一般是linux中的]
    import_dir.setFilter(QDir::Dirs|QDir::Files|QDir::NoDotAndDotDot);
    import_dir.setSorting(QDir::Name);
    QFileInfoList list = import_dir.entryInfoList();
    for(int i = 0; i < list.size(); i++)
    {
        if(_bstop)
        {
            return ;
        }
        QFileInfo fileInfo = list.at(i);
        bool bisDir = fileInfo.isDir();
        if(bisDir)
        {
            if(_bstop)
            {
                return ;
            }
            file_count++;
            emit SigUpdateProgress(file_count);
            QDir dist_dir(dist_path);
            QString sub_dist_path = dist_dir.absoluteFilePath(fileInfo.fileName());
            qDebug() << sub_dist_path;
            QDir sub_dist_dir(sub_dist_path);
            if(!sub_dist_dir.exists())
            {
                bool ok = sub_dist_dir.mkpath(sub_dist_path);
                if(!ok)
                {
                    continue;
                }
            }
            auto *item = new ProTreeItem(parent_item, fileInfo.fileName(), sub_dist_path, root, TreeItemDir);
            item->setData(0, Qt::DisplayRole, fileInfo.fileName());
            item->setData(0, Qt::DecorationRole, QIcon(":/icon/dir.png"));
            item->setData(0, Qt::ToolTipRole, sub_dist_path);
            CreateProTree(fileInfo.absoluteFilePath(), sub_dist_path, item, file_count,
                    self, root, pre_item);
        }
        else
        {
            if(_bstop)
            {
                return ;
            }
            const QString& suffix = fileInfo.completeSuffix();
            if(suffix != "png" && suffix != "jpeg" && suffix != "jpg")
            {
                continue;
            }
            file_count++;
            emit SigUpdateProgress(file_count);
            if(!needcopy){
                continue;
            }
            QDir dist_dir(dist_path);
            QString dist_file_path = dist_dir.absoluteFilePath(fileInfo.fileName());
            if(!QFile::copy(fileInfo.absoluteFilePath(), dist_file_path))
            {
                qDebug() << "copy failed";
                continue;
            }
            auto *item = new ProTreeItem(parent_item, fileInfo.fileName(), dist_file_path, root, TreeItemPic);
            item->setData(0, Qt::DisplayRole, fileInfo.fileName());
            item->setData(0, Qt::DecorationRole, QIcon(":/icon/pic.png"));
            item->setData(0, Qt::ToolTipRole, dist_file_path);

            //            有可能此时没有前一个节点，此时item是第一个节点
            if(pre_item)
            {
                auto *pre_proitem = dynamic_cast<ProTreeItem*>(pre_item);
                pre_proitem->SetNextItem(item);
            }

            item->SetPreItem(pre_item);
            pre_item = item;

        }
    }
}

void ProTreeThread::SlotCancleProgress()
{
    this->_bstop = true;
}
